class GameScene extends eui.Component implements  eui.UIComponent {
	private show1:eui.Image;
	private show2:eui.Image;
	private show3:eui.Image;
	private show4:eui.Image;
	private show5:eui.Image;
	private show6:eui.Image;
	private show7:eui.Image;
	private show8:eui.Image;
	private show9:eui.Image;
	private show10:eui.Image;
	private show11:eui.Image;
	private show12:eui.Image;
	private show13:eui.Image;
	private show14:eui.Image;
	private show15:eui.Image;
	private show16:eui.Image;
	private show17:eui.Image;
	private show18:eui.Image;
	private show19:eui.Image;
	private show20:eui.Image;
	private show21:eui.Image;
	private show22:eui.Image;
	private show23:eui.Image;
	private show24:eui.Image;
	private showpai:eui.Image[];
	private pkpai:Pai;
	private pai:string[];
	private player1pai:string[];
	private player2pai:string[];
	private btnstr:eui.Image;
	private showpainum1:eui.Label;
	private showpainum2:eui.Label;
	private showbacknum1:eui.Label;
	private maxspnum1:eui.Label;
	private showbacknum2:eui.Label;
	private maxspnum2:eui.Label;
	private maxsp1:number=0;
	private maxsp2:number=0;
	private player1xpnum:eui.Label;
	private player2xpnum:eui.Label;
	private btnxp:eui.Button;
	private btncp:eui.Button;
	private btnxp2:eui.Button;
	private btncp2:eui.Button;
	private xpnum2=3;
	private xpnum=3;
	private showpainum=0;
	private timer:egret.Timer=new egret.Timer(1000)
	private gametimer:GameTimer;
	private first=0;
	private player1statu:eui.Rect;
	private player2statu:eui.Rect;
	public constructor() {
		super();
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}


	protected childrenCreated():void
	{
		super.childrenCreated();
		this.gametimer=new GameTimer();
		this.player1pai=[]
		this.player2pai=[]
		this.btnstr.touchEnabled=true;
		this.btnxp.touchEnabled=true
		this.btncp.touchEnabled=true
		this.btnxp2.touchEnabled=true
		this.btncp2.touchEnabled=true
		this.showpai=[]
		this.pai=[]
		this.showpai.push(this.show1,this.show2,this.show3,this.show4,this.show5,this.show6,this.show7,this.show8,this.show9,this.show10,this.show11,this.show12,this.show13,this.show14,this.show15,this.show16,this.show17,this.show18,this.show19,this.show20,this.show21,this.show22,this.show23,this.show24)
		this.init();
		this.pkpai=new Pai();
		this.btnstr.addEventListener(egret.TouchEvent.TOUCH_TAP,this.gamestart,this)
		this.btnxp.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
			if(this.first==1){
			if(this.xpnum>0){
			this.xipai(this.player1pai)}
		}
		},this)
		this.btncp.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
			if(this.first==1){
			if(this.player1pai.length>0){
				this.addpai(this.player1pai)
			}
			this.first=2
		}
		},this)
		this.btncp2.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
			if(this.first==2){
			if(this.player2pai.length>0){
				this.addpai(this.player2pai)
			}
		     this.first=1
	     }
		},this)
		this.btnxp2.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
		    if(this.first==2){
			if(this.xpnum2>0){
				this.xipai(this.player2pai)
			}}
		},this)
		this.initpai();
	}
	 
	 private init(){
		 for(let i=0;i<this.showpai.length;i++){
			 this.showpai[i].source=""
		 }
	 }
     private gamestart(){
         this.first=this.gametimer.cptimer(this.gametimer.gamestart())
		 console.log(this.first)
		//  if(this.first==1){
		// 	 this.btncp.touchEnabled=true
		// 	 this.btnxp.touchEnabled=true
		//  }else if(this.first==2){
		// 	 this.btncp2.touchEnabled=true
		// 	 this.btnxp2.touchEnabled=true
		//  }
		 this.btnstr.visible=false
		 this.btnstr.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.gamestart,this)
		 this.fapai();
		 this.addEventListener(egret.Event.ENTER_FRAME,this.rungame,this)
	 }
	 private rungame(){
        this.showpainum1.text=JSON.stringify(this.player1pai.length)
		 this.showpainum2.text=JSON.stringify(this.player2pai.length)
		 this.player1xpnum.text="洗牌x"+JSON.stringify(this.xpnum)
		 this.player2xpnum.text="洗牌x"+JSON.stringify(this.xpnum2)
         if(this.showpainum==1){
		 for(let a=0;a<this.showpai.length;a++){
			 if(this.showpai[a].source!=""){
                 this.showpainum+=1
			 }
		 }
		 }
         
		if(this.first==1){
			this.player1statu.fillColor=0x28ff00
			this.player2statu.fillColor=0xff0000
		}else if(this.first==2){
			this.player1statu.fillColor=0xff0000
			this.player2statu.fillColor=0x28ff00
		}

		if(this.player1pai.length==0||this.player2pai.length==0){
			this.gameover();
		}
	 }
	 private gameover(){
		 if(this.player1pai.length==0){
			 console.log("玩家二获胜！")
		 }else {console.log("玩家一获胜！")}
		 this.btnxp.touchEnabled=false
		this.btncp.touchEnabled=false
		this.btnxp2.touchEnabled=false
		this.btncp2.touchEnabled=false
	 }
	 private addpai(a:string[]){
		 let playerpai;

		 if(a==this.player1pai){playerpai=1}
		 else if(a==this.player2pai){playerpai=2}
		 let d=a[0].substring(2,4)
		 let notempty:number=0;
		 for(let b=0;b<this.showpai.length;b++){
			 console.log("添加到牌框")
             if(this.showpai[b].source==""){
				 this.showpai[b].source=a[0]
				 notempty=b
				 break;
			 }
		 }
		 a.splice(0,1)
		 console.log(this.showpainum)
		 if(this.showpainum>1){
		 for(let c=1;c<=notempty;c++){
			//  if(this.showpai[c].source!=""){
            //    if(notempty==0){
			// 	   notempty=c
			//    }
			//      }
              let str
			  str=this.showpai[c-1].source
			  str=str.substring(2,4)
			  console.log(str,d)
			 
			if(str==d){
				console.log()
				console.log("玩家一牌量",a.length)
				console.log("有相同的牌")

				let e:string[];
				e=[]
				this.timer.start()
				this.btncp.touchEnabled=false
				this.btnxp.touchEnabled=false
				this.btncp2.touchEnabled=false
				this.btnxp2.touchEnabled=false
				let k=0
			this.timer.addEventListener(egret.TimerEvent.TIMER,function(){
			   if(k==0){
				   k=1
               for(let i=c-1;i<=notempty;i++){
				let src=JSON.stringify(this.showpai[i].source)
				src=src.substring(1,src.length-1)
                a.push(src)
				this.showpai[i].source=""
			}
			if(playerpai==1){
			  if((notempty-c+2)>this.maxsp1){
				  this.maxsp1=(notempty-c+2)
			 this.maxspnum1.text=JSON.stringify(this.maxsp1)
			  }
			 this.showbacknum1.text=JSON.stringify(JSON.parse(this.showbacknum1.text)+1)
			 this.first=1
			}
			else if(playerpai==2){
				if((notempty-c+2)>this.maxsp2){
					this.maxsp2=(notempty-c+2)
              this.maxspnum2.text=JSON.stringify(this.maxsp2)
				}
             this.showbacknum2.text=JSON.stringify(JSON.parse(this.showbacknum2.text)+1)
			 this.first=2
			}
			
			   }
			this.timer.stop();
			this.btncp.touchEnabled=true
			this.btnxp.touchEnabled=true
			this.btncp2.touchEnabled=true
			this.btnxp2.touchEnabled=true
			},this)
			
				// let e=(this.showpai).slice(c,notempty+1)
				// a.concat(e)
				//console.log(str,d)
				
			}
	   
		 }
		 }
	//	console.log(d)
		//console.log(notempty)
		 console.log("补位空的牌",this.showpainum)
		this.showpainum=1
		
	 }
	 private initpai(){
		 let b=0;
		
		 for(let a=51;a>=0;a--){
			 b=Math.ceil(Math.random()*this.pkpai.pai.length)-1
             this.pai.push(this.pkpai.pai[b])
			 this.pkpai.pai.splice(b,1)

		 }
       // for(let a=0;a<this.pai.length-1;a++){
		//	console.log(this.pai[a])
	//	}
	 }
     private xipai(a:string[]){
		 console.log("洗牌")
		  let b=0;
		  let num=a.length
		 let d:string[];
		 d=[]
		 if(a==this.player1pai){
			 this.xpnum-=1
		}else if(a==this.player2pai){
			this.xpnum2-=1
		}
		
		 for(let c=0;c<num;c++){
			 b=Math.ceil(Math.random()*a.length)-1
             d.push(a[b])
			 a.splice(b,1)
		 } 
		for(let z=0;z<d.length;z++){
			a.push(d[z])
		}
		
		for(let g=0;g<a.length;g++){
             console.log(a[g])
		}
	 }
	 private fapai(){
      this.player1pai=this.pai.slice(0,26)
	  this.player2pai=this.pai.slice(26,52)
	  console.log(this.player1pai.length)
	  console.log(this.player1pai.length)
	  console.log(this.pai[0],this.player1pai[0])
	  for(let g=0;g<this.player1pai.length;g++){
             console.log(this.player1pai[g])
		}
	 }
}